package com.angel.core.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.pdfbox.pdfparser.PDFParser;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.util.PDFTextStripper;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;

import com.angel.health.PDFEntity;
import com.angel.provider.pdf.ParsingPdf;
import com.github.pagehelper.StringUtil;
import com.lowagie.text.pdf.BaseFont;

public class PDFUtils {

	/**
	 * 读取PDF文件的文字内容
	 * 
	 * @param pdfPath
	 *            读取文件路径
	 * @param sort
	 *            是否排序
	 * @return
	 * @throws Exception
	 */
	public static String getTextFromPdf(String pdfPath, boolean sort) throws Exception {
		// 开始提取页数
		int startPage = 1;
		// 结束提取页数
		int endPage = Integer.MAX_VALUE;
		String content = null;
		InputStream input = null;
		File pdfFile = new File(pdfPath);
		PDDocument document = null;
		try {
			input = new FileInputStream(pdfFile);// 创建文件输入流
			PDFParser parser = new PDFParser(input);// 新建PDF解析器对象
			parser.parse(); // 对PDF文件进行解析
			document = parser.getPDDocument();// 获取解析后得到的PDF文档对象
			PDFTextStripper pts = new PDFTextStripper(); // 新建PDF文本剥离器
			pts.setSortByPosition(sort);
			endPage = document.getNumberOfPages();
			pts.setStartPage(startPage);
			pts.setEndPage(endPage);
			try {
				content = pts.getText(document);// 从PDF文档对象中剥离文本
			} catch (Exception e) {
				throw e;
			}
		} catch (Exception e) {
			throw e;
		} finally {
			if (null != input)
				input.close();
			if (null != document)
				document.close();
		}

		return content;
	}
	/**
	 * 生成pdf
	 * @param html
	 * @param path
	 */
	public static void generatePdf(String html,String path,String fileName,HttpServletRequest req){
		try {  
			
            ITextRenderer renderer = new ITextRenderer();  
            OutputStream os = null;
            ITextFontResolver fontResolver = renderer.getFontResolver(); 
//            fontResolver.addFont("C:/Windows/fonts/simsun.ttc", 
            fontResolver.addFont(path+"/font/simsun.ttc", 
                    BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 
              
            renderer.setDocumentFromString(html.toString());  
            // 解决图片的相对路径问题    
            renderer.getSharedContext().setBaseURL("file:///" + path + "/images/");  
            renderer.layout();  
            os = new FileOutputStream(path+"/pdf/"+fileName+".pdf");
            renderer.createPDF(os);  
            os.close();
        } catch (Exception e) {  
            e.printStackTrace();  
        }
	}
	
	public static void main(String[] args) {
        String pdfName = "E:\\最新angel/陈祥君.pdf";
        try {
        	String pdfAbove = getTextFromPdf(pdfName,false);
        	PDFEntity parsingAbove = ParsingPdf.parsingAbove(pdfAbove);
        	//解析下面体检结果  汇总分析、温馨提示  总检意见
			String pdfBelow = getTextFromPdf(pdfName,true);
			 Map<String, Object> parsingBelow = ParsingPdf.parsingBelow(pdfBelow);//体检结果
			 Map<String, Object> parsingTop = ParsingPdf.parsingTop(pdfBelow);//汇总分析、温馨提示  总检意见
			 parsingBelow.put("基础信息", parsingAbove);
			 parsingBelow.put("健康部分", parsingTop);
			 System.err.println(JsonUtil.toJson(parsingBelow));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

    }
}